package com.xtm.exercise.base.A14Singleton;

/**
 * 懒汉安全写法  实现线程安全
 */
public class SingleTon {
    // 静态实例变量加上volatile  通过volatile来防止指令重排
    private static volatile SingleTon instance = null;

    // 私有化构造函数
    private SingleTon() {

    }

    // 双重检查锁
    public static SingleTon getInstance() {
        if (instance == null) {
            synchronized(SingleTon.class){
                if(instance == null){
                    instance = new SingleTon();
                }
            }
        }
        return instance;
    }
}
